package com.YxyBackMange.Controller.web;

import com.YxyBackMange.Constant.SysConstant;
import com.YxyBackMange.Dto.Message;
import com.YxyBackMange.Entity.Sys.SysUser;
import com.YxyBackMange.Mapper.web.WebSysUserMapper;
import com.YxyBackMange.Service.web.WebSysUserService;
import com.YxyBackMange.Util.MD5;
import com.YxyBackMange.Util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @outhor 路♥克
 * 2017-12-05 19:52
 */
@Controller
@RequestMapping("/WebSysUser")
public class WebSysUserController {

    @Autowired
    private WebSysUserService webSysUserService;

    @Autowired
    private WebSysUserMapper webSysUserMapper;
    /**
     * @param sysUser 用户对象
     * @param phoneCode 验证码
     * @param session session对象
     * @return
     */
    @RequestMapping(value = "/insertSysUser",method = RequestMethod.POST)
    public  String insertSysUser(SysUser sysUser, Integer phoneCode, HttpServletRequest request, HttpSession session){
        Integer code = (Integer)session.getAttribute(sysUser.getPhone()+"code");
        if(code!=null && phoneCode!=null && code.equals(phoneCode)){
            SysUser resultUser = webSysUserService.insertService(sysUser);
            if (resultUser!=null) {
                request.setAttribute("message","注册成功");
                return "/web/success.jsp";
            }
        }
        return "/admin/500.html";
    }
    @RequestMapping(value = "/selectSysUser",method = RequestMethod.POST)
    public String selectSysUser(SysUser sysUser,HttpSession session){
        session.removeAttribute("user");
        Integer loginResult;
        //token加密
        String password= MD5.createMD5(sysUser.getToken());
        sysUser.setToken(null);
        //查询用户是否存在
        List<SysUser> userNameList =webSysUserMapper.select(sysUser);
        if (userNameList!=null && userNameList.size()>=1){
            //密码添加盐二次加密
            sysUser.setToken(MD5.createMD5(password+userNameList.get(0).getSalt()));
            List<SysUser> userList = webSysUserMapper.select(sysUser);
            if (userList!=null && userList.size()>=1){
                session.setAttribute("user",userList.get(0));
                SysUser updateVisitTime = new SysUser();
                //更新最后一次登陆的时间
                updateVisitTime.setIdentity(sysUser.getIdentity());
                updateVisitTime.setVisitTime(new Date());
                webSysUserService.update(updateVisitTime);
                loginResult =0;//正确
                return "/web/userCenter.jsp?loginResult="+loginResult;
            }else{
                loginResult =2;//密码错误
            }
        }else{
            loginResult =1;//用户名不正确
        }
        return "/Web/WebGoodsInfo/findAllGoodsInfo?loginResult="+loginResult;
    }
    @ResponseBody
    @RequestMapping(value = "/selectSysUserIdentity",method = RequestMethod.GET)
    public Boolean selectSysUserIdentity(SysUser SysUser){
        SysUser sysUser = webSysUserMapper.selectOne(SysUser);
        if(sysUser!=null){
            return  true;
        }
        return false;
    }
    @RequestMapping("/updateData")
    public String updateData(SysUser sysUser,HttpSession session,ModelMap map){
        System.out.println(sysUser.getEmail());
        SysUser sysUser1 = webSysUserService.updateService(sysUser);
        if(sysUser1 !=null){
            session.setAttribute("user",sysUser1);
        }
        map.put("updateDataMessage","修改资料成功，继续操作");
        return "/web/userCenter.jsp";
    }
    @RequestMapping("/updatePassword")
    public String updatePassword(SysUser sysUser,String code,HttpSession session,ModelMap map){
        Object attribute = session.getAttribute(sysUser.getPhone() + "code");
        if(code !=null &&attribute!=null && code.equals(attribute.toString())){
            String password= MD5.createMD5(sysUser.getToken());
            String salt= MyUtil.getRandomCharAndNumr(10);
            sysUser.setSalt(salt);
            sysUser.setToken(MD5.createMD5(password+salt));
            SysUser sysUser1 = webSysUserService.updateService(sysUser);
            if(sysUser1 !=null){
                session.removeAttribute("user");
                return "/Web/WebGoodsInfo/findAllGoodsInfo";
            }
        }
        map.put("updatePasswordMessage","修改密码失败");
        return "/web/securitySetting.jsp?updatePasswordMessage=修改密码失败";
    }
}
